/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ianaz.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author rainolsi
 */
@Entity
@NamedQueries({

    @NamedQuery(
        name = "Categoria.findByNome",
        query = "SELECT c FROM Categoria c WHERE c.nome = :nome"
     ),
     @NamedQuery(
        name = "Categoria.findSottoCategorie",
        query = "SELECT c FROM Categoria c WHERE c.padre = :padre"
     ),
     @NamedQuery(
        name = "Categoria.findCategoriePrincipali",
        query = "SELECT c FROM Categoria c WHERE c.padre = null"
     )
})
public class Categoria implements Serializable {
    private static long serialVersionUID = 1L;

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private Categoria padre = null;

    @OneToMany(mappedBy="padre", fetch=FetchType.EAGER)
    private List<Categoria> figli = new ArrayList(); 

    @OneToMany(mappedBy="categoria", fetch=FetchType.EAGER)
    private List<Documento> documenti = new ArrayList();
  
    @Column(name="nome")
    private String nome;


    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }

    public Categoria getPadre() {
        return padre;
    }

    public void setPadre(Categoria padre) {
        this.padre = padre;
    }

    public List<Categoria> getFigli() {
        return figli;
    }

    public void setFigli(List<Categoria> figli) {
        this.figli = figli;
    }

    public List<Documento> getDocumenti() {
        return documenti;
    }

    public void setDocumenti(List<Documento> documenti) {
        this.documenti = documenti;
    }
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Categoria)) {
            return false;
        }
        Categoria other = (Categoria) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.ianaz.bean.Categoria[id=" + getId() + "]";
    }


}
